package org.example.dao;

import org.example.utils.TextUtils;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultDirectedWeightedGraph;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.example.Main.textUtils;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class WordGraphTest {
    private WordGraph wordGraph;
    private ByteArrayOutputStream outputStreamCaptor;

    @BeforeEach
    public void setUp() {
        wordGraph = new WordGraph();
        outputStreamCaptor = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outputStreamCaptor));

        try {
            TextUtils textUtils = new TextUtils(); // Assuming TextUtils is a class that handles text processing
            String text = textUtils.processTextFile("src/main/java/org/example/input_txt/test.txt");
            wordGraph = new WordGraph(text);
        } catch (Exception e) {
            e.printStackTrace(); // Handle the exception properly based on your testing needs
        }
    }
    @Test
    void testCalcShortestPath_word_not_exist() {
        // Create a simple graph
        Graph<String, DefaultWeightedEdge> graph = new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class);
        ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outputStreamCaptor));
        // Test the method
        wordGraph.calcShortestPath("out", "worldds", "testFolder");
        // Verify output
        String expectedOutput = "No out or worldds in the graph!";
        assertEquals(expectedOutput, outputStreamCaptor.toString().trim());
    }
    @Test
    void testCalcShortestPath_path_invalid() {
        // Create a simple graph
        Graph<String, DefaultWeightedEdge> graph = new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class);
        ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outputStreamCaptor));
        // Test the method
        wordGraph.calcShortestPath("hope", "seek", "testFolder");
        // Verify output
        String expectedOutput = "No path found between hope and seek";
        assertEquals(expectedOutput, outputStreamCaptor.toString().trim());
    }
    @Test
    void testCalcShortestPath_path_valid_only() {
        // Create a simple graph
        Graph<String, DefaultWeightedEdge> graph = new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class);
        ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outputStreamCaptor));
        // Test the method
        wordGraph.calcShortestPath("to", "strange", "testFolder");
        // Verify output
        String expectedOutput = "目录已成功创建！\n" +
                "All shortest paths between to and strange:\n" +
                "Path:\n" +
                " - to -> explore\n" +
                " - explore -> strange\n" +
                "Path length: 2.0";
        assertEquals(expectedOutput, outputStreamCaptor.toString().trim());
    }
    @Test
    void testCalcShortestPath_path_valid_varies() {
        // Create a simple graph
        Graph<String, DefaultWeightedEdge> graph = new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class);
        ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outputStreamCaptor));
        // Test the method
        wordGraph.calcShortestPath("to", "civilizations", "testFolder");
        // Verify output
        String expectedOutput = "目录已成功创建！\n" +
                "All shortest paths between to and civilizations:\n" +
                "Path:\n" +
                " - to -> explore\n" +
                " - explore -> strange\n" +
                " - strange -> new\n" +
                " - new -> civilizations\n" +
                "Path length: 4.0\n" +
                "Path:\n" +
                " - to -> seek\n" +
                " - seek -> out\n" +
                " - out -> new\n" +
                " - new -> civilizations\n" +
                "Path length: 4.0";
        assertEquals(expectedOutput, outputStreamCaptor.toString().trim());
    }
}
